package com.gjh.controller.system;

import com.github.pagehelper.PageInfo;
import com.gjh.controller.BeanController;
import com.gjh.domain.system.Module;
import com.gjh.domain.system.Role;

import com.gjh.service.system.ModuleService;
import com.gjh.service.system.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BeanController{

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name = "展示角色列表数据")
    public String findAll(@RequestParam(name ="page",defaultValue = "1")int pageNum,@RequestParam(name = "pageSize",defaultValue = "10")int pageSize){
        PageInfo page= roleService.findByPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",page);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd" ,name = "进入新增角色界面")
    public String toAdd(){
        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit",name = "保存角色数据")
    public String edit(Role role){
        //判断Role中是否有id
        if(StringUtils.isEmpty(role.getId())){
           role.setId(UUID.randomUUID().toString());
           role.setCreateTime(new Date());
           role.setCompanyId(getCompanyId());
           role.setCompanyName(getComanyName());
           roleService.save(role);
        }else {
            role.setUpdateTime(new Date());
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入角色更新界面")
    public String toUpdate(String id){
        Role role=roleService.findById(id);
        request.setAttribute("role",role);
        return "system/role/role-update";
    }

    @RequestMapping(value = "/delete",name = "删除角色")
    public String deleteById(String id){
        roleService.deleteById(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule",name = "进入角色权限分配页面")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "/system/role/role-module";
    }

    @RequestMapping(value = "getZtreeNodes",name = "获取ztree需要的数据节点")
    @ResponseBody//直接将数据转成json 格式返回到调用的地方
    public List<Map> getZtreeNodes(String roleid){
        //获取该角色的所有权限
        List<String> moduleIdList = moduleService.findModulesByRoleId(roleid);
        //获取所有的module数据
        List<Module> moduleList = moduleService.findAll();
       // 1、所需要的数据是来自于moduleList  2、pId属性名对应不上  3、moduleList中有很多多余的属性
//       只需要 id  pId  name     map
        ArrayList<Map> list = new ArrayList<>();//用于接收返回的数据
        Map map=null;
        for (Module module : moduleList) {
            map=new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            if(moduleIdList.contains(module.getId())){
                map.put("checked",true);
            }
            list.add(map);
        }
        return list;
    }

    @RequestMapping(value = "/updateRoleModule",name = "修改角色权限")
    public String updateRoleModule(String roleid,String moduleIds){
        //前端返回的数据为roleid和，  "id,id,id,id,id"   这样的module id 的字符串中间由","隔开
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }



}
